package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.utils.AssertUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.business.bo.HcxPlatformProfitAddBo;
import com.ruoyi.business.bo.HcxPlatformProfitQueryBo;
import com.ruoyi.business.bo.HcxPlatformProfitEditBo;
import com.ruoyi.business.domain.HcxPlatformProfit;
import com.ruoyi.business.mapper.HcxPlatformProfitMapper;
import com.ruoyi.business.vo.HcxPlatformProfitVo;
import com.ruoyi.business.service.IHcxPlatformProfitService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 平台收益明细Service业务层处理
 *
 * @author wsdz
 * @date 2022-12-29
 */
@Service
public class HcxPlatformProfitServiceImpl extends ServicePlusImpl<HcxPlatformProfitMapper, HcxPlatformProfit> implements IHcxPlatformProfitService {

    @Override
    public HcxPlatformProfitVo queryById(Long id){
        return getVoById(id, HcxPlatformProfitVo.class);
    }

    @Override
    public TableDataInfo<HcxPlatformProfitVo> queryPageList(HcxPlatformProfitQueryBo bo) {
        PagePlus<HcxPlatformProfit, HcxPlatformProfitVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), HcxPlatformProfitVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<HcxPlatformProfitVo> queryList(HcxPlatformProfitQueryBo bo) {
        return listVo(buildQueryWrapper(bo), HcxPlatformProfitVo.class);
    }

    private LambdaQueryWrapper<HcxPlatformProfit> buildQueryWrapper(HcxPlatformProfitQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HcxPlatformProfit> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOrderId() != null, HcxPlatformProfit::getOrderId, bo.getOrderId());
        lqw.eq(StrUtil.isNotBlank(bo.getOrderNum()), HcxPlatformProfit::getOrderNum, bo.getOrderNum());
        lqw.eq(bo.getPrice() != null, HcxPlatformProfit::getPrice, bo.getPrice());
        lqw.eq(StrUtil.isNotBlank(bo.getDelFlag()), HcxPlatformProfit::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(HcxPlatformProfitAddBo bo) {
        HcxPlatformProfit add = BeanUtil.toBean(bo, HcxPlatformProfit.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(HcxPlatformProfitEditBo bo) {
        HcxPlatformProfit update = BeanUtil.toBean(bo, HcxPlatformProfit.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(HcxPlatformProfit entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importHcxPlatformProfit(List<HcxPlatformProfitVo> hcxPlatformProfitList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(hcxPlatformProfitList), "导入数据不能为空");
        int successNum = 0;
        for (HcxPlatformProfitVo hcxPlatformProfit : hcxPlatformProfitList) {
            HcxPlatformProfit add = BeanUtil.toBean(hcxPlatformProfit, HcxPlatformProfit.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", hcxPlatformProfitList.size(), successNum);
        if (successNum != hcxPlatformProfitList.size()) {
            msg = msg + String.format("失败 %d 条！", hcxPlatformProfitList.size() - successNum);
        }
        return msg;
    }
}
